<?php
$_helper = Mage::helper('catalog/output');
$_imageHelper = Mage::helper('catalog/image');
$_checkoutHelper = Mage::helper('checkout');
$_page = $this->getRequest()->getParam('page', 1);
$_categoryId = $this->getRequest()->getParam('category_id');
$_keyword = $this->getRequest()->getParam('keyword');
$_brand = $this->getRequest()->getParam('brand');
if(!$_categoryId) $_categoryId = 12;
$data = array();
//$layer = $this->getLayer();
$block = $this->getChild('catalog.filter');
//$html = $block->toHtml();
$filters = $block->getFilters();
$brands = array();
foreach($filters as $filter)
{
	$items = $filter->getItems();
	foreach($items as $item)
	{
		if($item->getFilter()->getRequestVar()=='brand')
		{
			$brands[] = array('name'=>$item->getLabel(),'value'=>$item->getValue());
		}
	}
}

$products = array();
$newIds = array();
//load new
if($_page == 1 && $_categoryId == 12 && !$_keyword && !$_brand)
{
	$_newProductCollection = Mage::getModel('catalog/product')->getCollection()
	            ->setPage(1,2)
	            ->setOrder("entity_id","desc")
	            ->addAttributeToSelect(array('name','small_image','price','tag','promotion'));
	Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this->_newProductCollection);
	Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($_newProductCollection);
	Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($_newProductCollection);

	$_newProductCollection->getSelect()->group('e.entity_id');
	foreach($_newProductCollection as $_product)
	{
		$products[] = array(
			'id'=>$_product->getId(),
			'name'=>$_product->getName(),
			'price'=>$_product->getPrice(),
			'special_price'=>$_product->getFinalPrice(),
			'image'=>$_imageHelper->init($_product, 'small_image')->resize(250)."",
			'promotion'=>$_product->getPromotion(),
			'tag'=>$_product->getAttributeText('tag')
			);
		$newIds[] = $_product->getId();
	}
}

$category = Mage::registry('current_category');
//echo $category->getName();
$collection = $this->getLoadedProductCollection();
$sql = $collection->getSelect()."";
//var_dump($sql);
foreach($collection as $_product)
{
	//echo $item->getName();
	if(in_array($_product->getId(), $newIds)) continue;

	$_cacheKey = "_product_".$_product->getId();
	$_data = apc_fetch($_cacheKey);
	if(!$_data)
	{
		$_data = array(
			'id'=>$_product->getId(),
			'name'=>$_product->getName(),
			'price'=>$_product->getPrice(),
			'special_price'=>$_product->getFinalPrice(),
			'image'=>$_imageHelper->init($_product, 'small_image')->resize(250)."",
			'promotion'=>$_product->getPromotion(),
			'tag'=>$_product->getAttributeText('tag')
		);
		apc_add($_cacheKey, $_data, 3600);
	}
	
	$products[] = $_data;
}



$data['page'] = $collection->getCurPage();
$data['total'] = $collection->getSize();
$data['totalPage'] = $collection->getLastPageNumber();
$data['products'] = $products;
$data['brands'] = $brands;
$json = json_encode(array('code'=>200,'msg'=>'','data'=>$data));
$callback = $this->getRequest()->getParam('callback');
$storeId = Mage::app()->getStore()->getId();
$getData = $_GET;
unset($getData['token']);
//print_r($getData);
$paramsKey = md5(json_encode($getData)).$storeId;
apc_add($paramsKey, $json, 3600);
if($callback)
{
	echo "{$callback}({$json})";
}
else
{
	echo $json;
}
?>